package com.my.chapter20;

import com.sun.mirror.apt.*;
import com.sun.mirror.declaration.*;
import net.mindView.annotation.ExtractInterface;

import java.io.*;
import java.util.*;

public class practice2 {
    public static void main(String[] args) {

    }
}

@ExtractInterface("IDivisor")
class Divisor {
    public int divide(int x, int y) {
        int total = 0;
        while(x >= y) {
            x = sub(x, y);
            total++;
        }
        return total;
    }
    private int sub(int x, int y) { return x - y; }
    public static void main(String[] args) {
        System.out.println(
                "2678/134 = " + new Divisor().divide(2678, 134));
    }
}

//处理器类
class InterfaceExtractorProcessor implements AnnotationProcessor {
    private final AnnotationProcessorEnvironment env;
    private ArrayList<MethodDeclaration> interfaceMethods =
            new ArrayList<MethodDeclaration>();
    public InterfaceExtractorProcessor(
            AnnotationProcessorEnvironment env) { this.env = env; }
    public void process() {
        for(TypeDeclaration typeDecl :
                env.getSpecifiedTypeDeclarations()) {
            interfaceMethods.clear();
            ExtractInterface annot =
                    typeDecl.getAnnotation(ExtractInterface.class);
            if(annot == null)
                break;
            for(MethodDeclaration m : typeDecl.getMethods())
                if(m.getModifiers().contains(Modifier.PUBLIC) &&
                        !(m.getModifiers().contains(Modifier.STATIC)))
                    interfaceMethods.add(m);
            if(interfaceMethods.size() > 0) {
                try {
                    PrintWriter writer =
                            env.getFiler().createSourceFile(annot.value());
                    writer.println("package " +
                            typeDecl.getPackage().getQualifiedName() +";");
                    writer.println("public interface " +
                            annot.value() + " {");
                    for(MethodDeclaration m : interfaceMethods) {
                        writer.print(" public ");
                        writer.print(m.getReturnType() + " ");
                        writer.print(m.getSimpleName() + " (");
                        int i = 0;
                        for(ParameterDeclaration parm :
                                m.getParameters()) {
                            writer.print(parm.getType() + " " +
                                    parm.getSimpleName());
                            if(++i < m.getParameters().size())
                                writer.print(", ");
                        }
                        writer.println(");");
                    }
                    writer.println("}");
                    writer.close();
                }catch(IOException ioe) {
                    throw new RuntimeException(ioe);
                }
            }
        }
    }
}

//apt工具需要一个工厂类来为其指明正确的处理器，然后才能调用处理器上的process()方法
class InterfaceExtractorProcessorFactory implements AnnotationProcessorFactory {

    @Override
    public Collection<String> supportedOptions() {
        return  Collections.emptySet();
    }

    //返回注解的完整类名
    @Override
    public Collection<String> supportedAnnotationTypes() {
        return Collections.singleton("net.mindView.annotation.ExtractInterface");
    }

    //返回注解处理器
    @Override
    public AnnotationProcessor getProcessorFor(Set<AnnotationTypeDeclaration> atds, AnnotationProcessorEnvironment env) {
        return new InterfaceExtractorProcessor(env);
    }
}


